package com.cdh.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cdh.base.DIYException;
import com.cdh.sms.service.SmsService;
import com.cdh.sms.util.SmsProperties;
import com.cdh.utils.result.ResultCodeEnum;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author by cdh
 * @description:
 * @Date: Created in 10:47 on 2021/3/3
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    private static final String SUCCESS = "OK";
    private static final String BUSINESS_LIMIT_CONTROL = "isv.BUSINESS_LIMIT_CONTROL";
    @Autowired
    private SmsProperties smsProperties;

    @Override
    public void send(String phone, String verificationCode) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(
                smsProperties.getRegionId(),
                smsProperties.getKeyId(),
                smsProperties.getKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", smsProperties.getRegionId());
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", smsProperties.getSignName());
        request.putQueryParameter("TemplateCode", smsProperties.getTemplateCode());

        Map<String,String> map = new HashedMap<>(16);
        map.put("code", verificationCode);
        Gson gson = new Gson();
        String templateParam = gson.toJson(map);

        request.putQueryParameter("TemplateParam", templateParam);

        // 发送短信
        CommonResponse response = client.getCommonResponse(request);

        // 得到返回结果
        String responseData = response.getData();

        // 解析响应结果
        Map<String,String> data = gson.fromJson(responseData, HashMap.class);
        String code = data.get("code");
        String message = data.get("message");

        if (!SUCCESS.equals(code)){
            log.error("短信发送失败：" + "code:" + code + ",message" + message);
            throw new DIYException(ResultCodeEnum.SMS_SEND_ERROR.getCode(),
                    ResultCodeEnum.SMS_SEND_ERROR.getMessage());
        }

        if (!BUSINESS_LIMIT_CONTROL.equals(code)){
            log.error("短信发送失败：" + "code:" + code + ",message" + message);
            throw new DIYException(ResultCodeEnum.SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL.getCode(),
                    ResultCodeEnum.SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL.getMessage());
        }
    }
}
